package com.tjec.project.system.controller;

import com.tjec.common.constant.Constants;
import com.tjec.common.utils.poi.ExcelUtil;
import com.tjec.framework.aspectj.lang.annotation.Log;
import com.tjec.framework.aspectj.lang.enums.BusinessType;
import com.tjec.framework.web.controller.BaseController;
import com.tjec.framework.web.domain.AjaxResult;
import com.tjec.framework.web.page.TableDataInfo;
import com.tjec.project.system.domain.BmsMdArchiveTemplate;
import com.tjec.project.system.domain.vo.BmsAttachmentVo;
import com.tjec.project.system.service.IBmsAttachmentService;
import com.tjec.project.system.service.IBmsMdArchiveTemplateService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 归档模板Controller
 *
 * @author tjec
 * @date 2020-04-16
 */
@RestController
@RequestMapping("/system/archiveTemplate")
public class BmsMdArchiveTemplateController extends BaseController {
    @Value("${template.url}")
    private String url;
    @Value("${template.archive_template}")
    private String template;
    @Value("${tjec.profile}")
    private String profile;

    @Autowired
    private IBmsMdArchiveTemplateService bmsMdArchiveTemplateService;
    @Autowired
    private IBmsAttachmentService iBmsAttachmentService;

    /**
     * 查询归档模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:archiveTemplate:list')")
    @GetMapping("/list")
    public TableDataInfo list(BmsMdArchiveTemplate bmsMdArchiveTemplate) {
        startPage();
        List<BmsMdArchiveTemplate> list = bmsMdArchiveTemplateService.selectBmsMdArchiveTemplateList(bmsMdArchiveTemplate);
        return getDataTable(list);
    }

    /**
     * 导出归档模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:archiveTemplate:export')")
    @Log(title = "归档模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BmsMdArchiveTemplate bmsMdArchiveTemplate) {
        List<BmsMdArchiveTemplate> list = bmsMdArchiveTemplateService.selectBmsMdArchiveTemplateList(bmsMdArchiveTemplate);
        ExcelUtil<BmsMdArchiveTemplate> util = new ExcelUtil<BmsMdArchiveTemplate>(BmsMdArchiveTemplate.class);
        return util.exportExcel(list, "archiveTemplate");
    }

    /**
     * 下载模板文件
     */
    @PreAuthorize("@ss.hasPermi('system:archiveTemplate:export')")
    @Log(title = "下载模板文件", businessType = BusinessType.EXPORT)
    @GetMapping("/exportTemplate")
    public AjaxResult exportTemplate(HttpServletRequest request) {
        Map map = new HashMap<>();
        map.put("url", Constants.RESOURCE_PREFIX + url + "/" + template);
        return AjaxResult.success(map);
    }

    /**
     * 获取归档模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:archiveTemplate:query')")
    @GetMapping(value = "/{guid}")
    public AjaxResult getInfo(@PathVariable("guid") String guid) {
        return AjaxResult.success(bmsMdArchiveTemplateService.selectBmsMdArchiveTemplateById(guid));
    }

    /**
     * 新增归档模板
     */
    @PreAuthorize("@ss.hasPermi('system:archiveTemplate:add')")
    @Log(title = "归档模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmsMdArchiveTemplate bmsMdArchiveTemplate) {
        return toAjax(bmsMdArchiveTemplateService.insertBmsMdArchiveTemplate(bmsMdArchiveTemplate));
    }

    /**
     * 修改归档模板
     */
    @PreAuthorize("@ss.hasPermi('system:archiveTemplate:edit')")
    @Log(title = "归档模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmsMdArchiveTemplate bmsMdArchiveTemplate) {
        return toAjax(bmsMdArchiveTemplateService.updateBmsMdArchiveTemplate(bmsMdArchiveTemplate));
    }

    /**
     * 删除归档模板
     */
    @PreAuthorize("@ss.hasPermi('system:archiveTemplate:remove')")
    @Log(title = "归档模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        return toAjax(bmsMdArchiveTemplateService.deleteBmsMdArchiveTemplateByIds(guids));
    }

    /**
     * 验证文件数据格式
     */
    @GetMapping(value = "/validation")
    public AjaxResult validation(String guid) throws Exception {
        //根据id获取文件路径
        List<BmsAttachmentVo> list = iBmsAttachmentService.selectBmsAttachmentDownloadabsolute(guid);
        if (CollectionUtils.isEmpty(list)) {
            AjaxResult.error("系统不存在该文件");
        }
        String fullPath = profile + list.get(0).getStoragePath();
        return bmsMdArchiveTemplateService.validation(fullPath);
    }
}
